################################
#	John Henderson
#	Gerrymandering Incumbency 
#		(with Brian Hamel and Aaron Goldzimer)
#		April 9, 2017    
#
################################
# code edits that produced by Chen & Rodden (2013), to replicate their 2000 analysis 
#  for competitiveness outcomes                                                 
################################
# calls sims2000.R to load data for plotting

rm(list=ls())    

GetColorHexAndDecimal <- function(color)
{
  c <- col2rgb(color)
  sprintf("#%02X%02X%02X %3d %3d %3d", c[1],c[2],c[3], c[1], c[2], c[3])
}

library(stringr)  
library(foreign)

setwd("~/Dropbox/StateRedistricting/replication/short/")
source('sims2010.R')

#########################
# cd vertical
#########################

##tt.e08
##tt.cds   
     
propGT=matrix(NA,length(states_ix),3) 

#{
#pdf(width=5,height=9,file='~/Dropbox/StateRedistricting/Figures/simFigures/cd_2010-vert.pdf')
#plot(x=-1000, y=-1000, type='n', xlim=c(-.09, .5), ylim=c(.5,43), axes=F, ylab="", xlab="")#, main=paste("Margins of Victory in House Elections\n",state,sep=""))
#axis(1,at=c(0,.1,.2,.3,.4,.5),labels=c(0,.1,.2,.3,.4,.5))
#cnts=0
cnts=44
#Means and Medians
     
ss.dif=c()
ss.mu=c()

for (state in states_ix){
    
    if(length(which(tt.e08[,1]==state))>0){
		ee=sort(as.numeric(tt.e08[which(tt.e08[,1]==state),2]))
		ff=as.data.frame(tt.cds[which(tt.cds[,1]==state),-c(1)])
		for(i in 1:ncol(ff)){
			ff[,i]=as.numeric(sort(as.numeric(as.character(ff[,i]))))
		}
	} else {
		ee=ff=NA
	}

	iq=which(state==states_ix) 
	cnts=cnts-1
	
	#Means :: average over simulatons, i.e., counterfactual districtings
	means.sim=as.numeric(sims[which(sims[,1]==state),2:ncol(sims)])
	mean.10=election08[which(names(election08)==state)]
	
	#hypothesis-test-like statistics
	if(length(mean.10)>0){
		mu=mean(means.sim,na.rm=T)
		sds=sd(means.sim,na.rm=T) 
		s.means.sim=((means.sim-mu)/sds)
		s.mean.10=((mean.10-mu)/sds)	
		s.dif=mean(s.mean.10-s.means.sim)
		if(s.dif>0){
			p.dif=sum(s.mean.10<s.means.sim)/length(s.means.sim)    
		} else{                                                     
			p.dif=sum(s.mean.10>s.means.sim)/length(s.means.sim)    
		}
	} else{
		p.dif=1
		s.dif=NA
	}     
	      
	ss.dif=c(ss.dif,s.dif) 
	ss.mu=c(ss.mu,mean.10-mu)
	
	#t.sim=temp.sims
	#for(i in 1:nrow(t.sim)){
	#	t.sim[i,which(is.na(t.sim[i,]))]=mean(t.sim[i,which(!is.na(t.sim[i,]))],na.rm=T)
	#	t.sim[i,]=sort(t.sim[i,])
	#}
	#t.02=(sort(temp.02))
    #t.sim=t(t.sim)  

	propGT[iq,2]=mean(ee>ff)
	
	#cols='red' 
	#ltys=2
	#if(p.dif<.05){
	#	cols='red' 
	#	ltys=1
	#}
	
	#if(length(mean.10)>0){ 	 
	#	points(x=means.sim, y=jitter(factor=.2,rep(cnts,length(means.sim))), col=gray(.65), lwd=2,pch=20,cex=.52)
	#	points(y=cnts, x=mean.10, col=cols, pch=1,cex=sqrt(1+abs(s.dif))/1.25,lty=2) 	
	#  			
	#	text(col='black',adj=c(0,NA),x=.004,y=cnts,labels=paste('(',str_sub(s.dif,1,str_locate(s.dif,pattern='[/.]')[1]+2),')',sep=''),srt = 1, pos = 2,cex=.5)				
	#} else{
	#	text(col='black',adj=c(0,NA),x=.004,y=cnts,labels='NA',srt = 1, pos = 2,cex=.5)				
	#}                                                                                           
	
	#text(adj=c(0,NA),x=-.055,y=cnts,labels=as.character(state),srt = 1, pos = 2,cex=.65)
	propGT[iq,1]=mean(means.sim<mean.10) 


	#propGT[iq,3]=mean(abs(app))-mean(abs(opp))
	#propGT[iq,4]=mean(mean(abs(app))>(colMeans(abs(opp))))
}         
                            
# lines and legend graphics   
#lines(col='grey',lty=2,y=c(.5,.5),x=c(-.0515,.5))
#lines(col='grey',lty=2,y=c(4.5,4.5),x=c(-.0515,.5))  
#lines(col='grey',lty=2,y=c(13.5,13.5),x=c(-.0515,.5))
#lines(col='grey',lty=2,y=c(21.5,21.5),x=c(-.0515,.5))
#lines(col='grey',lty=2,y=c(37.5,37.5),x=c(-.0515,.5))
#lines(col='grey',lty=2,y=c(43.5,43.5),x=c(-.0515,.5))                              
                                          
#axis(2,line=-1.5,at=c(.5,4.5,13.5,21.5,37.5,43.5),labels=F,tck=-.02)

##text(x=.5-.0105,y=42.,labels='Democratic',cex=.75,adj=c(1,1),srt=0)
#text(x=.5-.0105,y=36.,labels='Republican',cex=.75,adj=c(1,1),srt=0)
#text(x=.5-.0105,y=20.,labels='Bipartisan',cex=.75,adj=c(1,1),srt=0)
#text(x=.5-.0105,y=12.,labels='Court',cex=.75,adj=c(1,1),srt=0) 
#text(x=.5-.0105,y=3.,labels='Independent',cex=.75,adj=c(1,1),srt=0) 
#mtext("Mean Margin of Victory",side=1,at=.25,adj=.5,line=3)
#dev.off()
#}   

pl10=array(NA,nrow(propGT))
for(i in 1:nrow(propGT)){	
	pl10[i]=plansCD10[which(states==propGT[i,3])]
}
propGT=as.data.frame(propGT)
propGT[,1]=as.numeric(as.character(propGT[,1]))
propGT[,2]=as.numeric(as.character(propGT[,2]))

t.test(propGT[pl10=='I',1],c(propGT[pl10=='B',1],propGT[pl10=='D',1],propGT[pl10=='R',1]))
#t.test(propGT[pl10=='I',2],c(propGT[pl10=='B',2],propGT[pl10=='D',2],propGT[pl10=='R',2]))                                                                                          
#t.test(c(propGT[pl10=='I',1],propGT[pl10=='I',2]),c(propGT[pl10=='B',1],propGT[pl10=='D',1],propGT[pl10=='R',1],propGT[pl10=='B',2],propGT[pl10=='D',2],propGT[pl10=='R',2]))

                                                                                         
#t.test(propGT[pl10=='C',1],c(propGT[pl10=='B',1],propGT[pl10=='D',1],propGT[pl10=='R',1]))
#t.test(propGT[pl10=='C',2],c(propGT[pl10=='B',2],propGT[pl10=='D',2],propGT[pl10=='R',2]))
t.test(c(propGT[pl10=='C',1],propGT[pl10=='C',2]),c(propGT[pl10=='B',1],propGT[pl10=='D',1],propGT[pl10=='R',1],propGT[pl10=='B',2],propGT[pl10=='D',2],propGT[pl10=='R',2]))       
 
#t.test(propGT[pl10=='I',1],c(propGT[pl10=='C',1]))
#t.test(propGT[pl10=='I',2],c(propGT[pl10=='C',2]))
#t.test(c(propGT[pl10=='I',1],propGT[pl10=='I',2]),c(propGT[pl10=='C',1],propGT[pl10=='C',2]))
                                                                                                                                                                                   
#end